﻿using BLL.BatchBLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDMUI
{
    public partial class createBatch : Form
    {
        private createBatchBLL createBatchBLL = new createBatchBLL();
        private List<Batch> createdBatches = new List<Batch>();

        public createBatch()
        {
            InitializeComponent();
            InitializeDataGridView();
            InitializeDataGridView3();
        }

        // 初始化 DataGridView1 的表头
        private void InitializeDataGridView()
        {
            // 清空现有列
            dataGridView1.Columns.Clear();

            // 设置 DataGridView 的属性
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // 设置选择模式为整行选择

            // 添加列
            DataGridViewTextBoxColumn colWorkOrderId = new DataGridViewTextBoxColumn();
            colWorkOrderId.Name = "WorkOrderId";
            colWorkOrderId.HeaderText = "工单号";
            colWorkOrderId.DataPropertyName = "WorkOrderId";
            colWorkOrderId.Width = 150;
            dataGridView1.Columns.Add(colWorkOrderId);

            DataGridViewTextBoxColumn colWorkOrderType = new DataGridViewTextBoxColumn();
            colWorkOrderType.Name = "WorkOrderType";
            colWorkOrderType.HeaderText = "工单类型";
            colWorkOrderType.DataPropertyName = "WorkOrderType";
            colWorkOrderType.Width = 120;
            dataGridView1.Columns.Add(colWorkOrderType);

            DataGridViewTextBoxColumn colDetailedType = new DataGridViewTextBoxColumn();
            colDetailedType.Name = "DetailedType";
            colDetailedType.HeaderText = "详细类型";
            colDetailedType.DataPropertyName = "DetailedType";
            colDetailedType.Width = 120;
            dataGridView1.Columns.Add(colDetailedType);

            DataGridViewTextBoxColumn colWorkOrderDescription = new DataGridViewTextBoxColumn();
            colWorkOrderDescription.Name = "WorkOrderDescription";
            colWorkOrderDescription.HeaderText = "工单描述";
            colWorkOrderDescription.DataPropertyName = "WorkOrderDescription";
            colWorkOrderDescription.Width = 150;
            dataGridView1.Columns.Add(colWorkOrderDescription);

            DataGridViewTextBoxColumn colWorkOrderStatus = new DataGridViewTextBoxColumn();
            colWorkOrderStatus.Name = "WorkOrderStatus";
            colWorkOrderStatus.HeaderText = "工单状态";
            colWorkOrderStatus.DataPropertyName = "WorkOrderStatus";
            colWorkOrderStatus.Width = 120;
            dataGridView1.Columns.Add(colWorkOrderStatus);


            DataGridViewTextBoxColumn colPlannedStartDate = new DataGridViewTextBoxColumn();
            colPlannedStartDate.Name = "PlannedStartDate";
            colPlannedStartDate.HeaderText = "计划开始日期";
            colPlannedStartDate.DataPropertyName = "PlannedStartDate";
            colPlannedStartDate.Width = 120;
            dataGridView1.Columns.Add(colPlannedStartDate);

            DataGridViewTextBoxColumn colPlannedDueDate = new DataGridViewTextBoxColumn();
            colPlannedDueDate.Name = "PlannedDueDate";
            colPlannedDueDate.HeaderText = "计划到期日期";
            colPlannedDueDate.DataPropertyName = "PlannedDueDate";
            colPlannedDueDate.Width = 120;
            dataGridView1.Columns.Add(colPlannedDueDate);

            DataGridViewTextBoxColumn colPlannedQuantity = new DataGridViewTextBoxColumn();
            colPlannedQuantity.Name = "PlannedQuantity";
            colPlannedQuantity.HeaderText = "计划数量";
            colPlannedQuantity.DataPropertyName = "PlannedQuantity";
            colPlannedQuantity.Width = 100;
            dataGridView1.Columns.Add(colPlannedQuantity);

            // 调整列的自动填充模式
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        // 初始化 DataGridView3 的表头
        private void InitializeDataGridView3()
        {
            // 清空现有列
            dataGridView3.Columns.Clear();

            // 设置 DataGridView 的属性
            dataGridView3.AutoGenerateColumns = false;
            dataGridView3.ReadOnly = true;
            dataGridView3.AllowUserToAddRows = false;
            dataGridView3.AllowUserToDeleteRows = false;
            dataGridView3.RowHeadersVisible = false;

            // 添加列
            DataGridViewTextBoxColumn colBatchId = new DataGridViewTextBoxColumn();
            colBatchId.Name = "BatchId";
            colBatchId.HeaderText = "批次号";
            colBatchId.DataPropertyName = "BatchId";
            colBatchId.Width = 150;
            dataGridView3.Columns.Add(colBatchId);

            DataGridViewTextBoxColumn colBatchQuantity = new DataGridViewTextBoxColumn();
            colBatchQuantity.Name = "Quantity";
            colBatchQuantity.HeaderText = "批次数量";
            colBatchQuantity.DataPropertyName = "Quantity";
            colBatchQuantity.Width = 150;
            dataGridView3.Columns.Add(colBatchQuantity);

            // 调整列的自动填充模式
            dataGridView3.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        // 查询按钮点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            string workOrderIdStr = textBox1.Text.Trim();

            if (string.IsNullOrEmpty(workOrderIdStr))
            {
                // 如果没有输入工单号，则查询所有工单数据
                List<WorkOrder> GridViewData = createBatchBLL.GetAllWorkOrder();
                if (GridViewData != null && GridViewData.Count > 0)
                {
                    dataGridView1.DataSource = GridViewData;
                }
                else
                {
                    MessageBox.Show("未获取到工单数据，请检查数据库连接或工单信息！");
                }
            }
            else
            {
                // 如果输入了工单号，则根据工单号查询
                int workOrderId;
                if (!int.TryParse(workOrderIdStr, out workOrderId))
                {
                    MessageBox.Show("输入的工单号必须是整数！");
                    return;
                }

                WorkOrder workOrder = createBatchBLL.GetWorkOrderById(workOrderId);
                if (workOrder != null)
                {
                    // 将查询到的单个工单数据转换为列表并显示
                    List<WorkOrder> GridViewData = new List<WorkOrder> { workOrder };
                    dataGridView1.DataSource = GridViewData;
                }
                else
                {
                    MessageBox.Show("未找到指定的工单，请检查输入的工单号是否正确！");
                }
            }
        }

        // 创建批次按钮点击事件
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择一个工单！");
                return;
            }

            // 获取选中的工单号和状态
            string selectedWorkOrderIdStr = dataGridView1.SelectedRows[0].Cells["WorkOrderId"].Value.ToString();
            string selectedWorkOrderStatus = dataGridView1.SelectedRows[0].Cells["WorkOrderStatus"].Value.ToString();

            if (!int.TryParse(selectedWorkOrderIdStr, out int selectedWorkOrderId))
            {
                MessageBox.Show("工单号必须是整数！");
                return;
            }

            // 判断工单状态
            if (selectedWorkOrderStatus != "已投单")
            {
                MessageBox.Show("所选工单未投产，无法创建批次！");
                return;
            }

            // 创建并显示批次创建窗体
            using (BatchCreationForm batchCreationForm = new BatchCreationForm(selectedWorkOrderId))
            {
                if (batchCreationForm.ShowDialog() == DialogResult.OK)
                {
                    // 如果用户点击了确定，获取创建的批次信息并添加到列表中
                    Batch createdBatch = batchCreationForm.CreatedBatch;

                    if (createdBatch != null)
                    {
                        // 更新 createdBatches 列表
                        createdBatches.Add(createdBatch);

                        // 更新 DataGridView3
                        dataGridView3.DataSource = null; // 先清空数据源
                        dataGridView3.DataSource = createdBatches;

                    }
                }
            }
        }

        // 取消创建批次按钮点击事件
        private void button3_Click(object sender, EventArgs e)
        {
            // 清空输入框
            textBox1.Clear();

            // 清空 DataGridView3
            createdBatches.Clear();
            dataGridView3.DataSource = null;
        }
    }
}